import Mock, { Random } from "mockjs"; // 导出随机函数

// 在生成多条数据时，不要用Random.xxx()方法，因为该种方法生成的列表数据会重复，这里用@语法，如：@id()
let blog = {
  id: '@id()',
  title: '@ctitle(5, 15)',
  author: '@cname()',
  date: '@date()',
  content: '@cparagraph(10, 20, 5)',
  coverImage: "@image('300x200', '@color()', '@color()', '枫叶')",
  'comments|0-5': [
    {
        id: '@id()',
        author: '@cname()',
        date: '@date()',
        content: '@cparagraph(1, 5)'
    }
  ]
}

let articles = (req) => {
   // 如果不用Mock.mock方法处理一下，直接返回对象，最外层识别不了模版语法，比如 'data|5-10'
  return Mock.mock({
    code: 200,
    'data|6-10': [blog],
    message: "OK"
  })
}

let articleList = Mock.mock({
  'list|100': [blog]
})

let articlePages = (req) => {
  // req 是一个请求对象，包含: url，type和body属性
  // console.log("分页请求对象：", req);
  let params = JSON.parse(req.body);
  const [currentPage, size, total] = [params.currentPage, params.size, articleList.list.length];

  let start = (currentPage - 1) * size;
  let end = start + size;
  if (end > total) end = total;

  return{
    code: 200,
    message: "OK",
    data: {
      'currentPage': currentPage,
      'size': size,
      'total': total,
      'totalPage': Math.ceil(total / size),
      'list': articleList.list.slice(start, end)
    }
  }
}

let hotBlogs = () => {
  // 如果不用Mock.mock方法处理一下，直接返回对象，最外层识别不了模版语法，比如 'data|5-10'
  return Mock.mock({
    code: 200,
    data: {
      'list|5': [blog]
    },
    message: "OK"
  })
}

let blogDetail = (req) => {
  let params = JSON.stringify(req.body);
  let id = params.id;
  return Mock.mock({
    code: 200,
    message: "OK",
    data: blog
  })
}

export default {
  articles,
  articlePages,
  hotBlogs,
  blogDetail,
};
